package com.bjsxt;
/**工作中有几个线程，逻辑上其中一个线程要等待，就用join*/

class sonThread implements Runnable{
    @Override
    public void run() {
        System.out.println("儿子去买烟");
        System.out.println("买烟需要10分钟");
        for(int i=1;i<11;i++){
            System.out.println("第"+i+"分钟");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
        }
        System.out.println("儿子买烟回来");
    }
}
class fatherThread implements Runnable{

    @Override
    public void run() {
        System.out.println("爸爸想抽烟，发现烟没了");
        System.out.println("爸爸让儿子去买烟");
        //启动儿子线程
        Thread t = new Thread(new sonThread());
        t.start();
        //爸爸在等待，线程阻塞状态
        System.out.println("等侍儿子回来");
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("爸爸去找儿子");
            System.exit(1);
        }
        System.out.println("爸爸抽烟，并把零钱给儿子");
    }
}
public class JoinDemo {
    public static void main(String[] args) {
        System.out.println("爸爸和儿子买烟的故事");
        //实例化爸爸线程
        Thread thread = new Thread(new fatherThread());
        thread.start();
    }
}
